//
//  APBaseNavigationControllerViewController.m
//  AqualPolo
//
//  Created by dong on 2024/5/1.
//

#import "APBaseNavigationController.h"

@interface APBaseNavigationController ()
@property (nonatomic, strong) UIButton *backButton;

@end

@implementation APBaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationBar setShadowImage:[UIImage new]];
    UIImageView *iv = [self findHairlineImageViewUnder:self.navigationBar];
    iv.hidden = YES;
    self.navigationItem.hidesBackButton = YES;
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationBar.translucent = false;
    self.navigationBar.tintColor = [UIColor blackColor];
}


// 寻找导航栏下的黑线
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if ([self.viewControllers count] > 0){
        viewController.hidesBottomBarWhenPushed = YES;
        if (!viewController.view.backgroundColor) {
            viewController.view.backgroundColor = [UIColor whiteColor];
        }
        //可以在这里定义返回按钮等
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
         viewController.navigationItem.leftBarButtonItem = backItem;
        viewController.navigationItem.hidesBackButton = YES;
//        viewController.navigationItem.backBarButtonItem = backItem;
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    //一定要写在最后，要不然无效
    [super pushViewController:viewController animated:animated];
    //处理了push后隐藏底部UITabBar的情况，并解决了iPhonX上push时UITabBar上移的问题。
    CGRect rect = self.tabBarController.tabBar.frame;
    rect.origin.y = [UIScreen mainScreen].bounds.size.height - rect.size.height;
    self.tabBarController.tabBar.frame = rect;
}

- (UIButton *)backButton{
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [UIImage imageNamed:@"back_blue"];
        [_backButton setImage:image forState:UIControlStateNormal];
        _backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        _backButton.frame = CGRectMake(0, 0, 28, 28);
        [_backButton setTitle:@"" forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}

- (void)backButtonAction:(UIButton *)sender{
    [self popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate{
    return YES;
}

@end
